package gov.fema.hazus.bdc.client.common.ui;

import java.util.Date;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.TextBox;

public class TextBoxDateValueBlurHandler implements BlurHandler {

	@SuppressWarnings("deprecation")
	@Override
	public void onBlur(BlurEvent event) {
		
		TextBox tb = (TextBox) event.getSource();
		try {
			DateTimeFormat f = DateTimeFormat.getFormat("MM/dd/yyyy");
			Date dateValue = f.parse(tb.getText());
			Message.get().setText("");
			Message.get().hide();
			Message.get().removeInvalidField(tb.getName());
			tb.removeStyleName("validation-warning");
		} catch (Exception e) { // NumberFormat and IllegalArgument
			tb.addStyleName("validation-warning");
			Message.get().addInvalidField(tb.getName());
			Message.get().setText("The "+tb.getName()+" field be a valid date (MM/dd/yyyy).");
			Message.get().show();
		}
	}
}
